package com.yss.reportworld.domain.rule;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.yss.common.core.annotation.EasyExcel;
import com.yss.common.core.annotation.ExcelValid;
import com.yss.common.core.utils.easyexecl.IntegerConverter;
import com.yss.common.core.utils.easyexecl.StringConverter;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 规则校验_校验对象_规则对象 rw_db_r_check_rule
 *
 * @author yss
 * @date 2023-07-26
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("rw_db_r_check_rule")
public class RCheckRule extends Model<RCheckRule> {
    public static final String SHEET_NAME = "规则列表";
    /**
     * ID
     */
    @ExcelIgnore
    @TableId("ID")
    private String id;


    @ExcelProperty({"规则配置", "校验对象"})
    @TableField(exist = false)
    @ExcelValid(message = "【规则列表】【规则配置】【校验对象】不能为空")
    private String checkCode;


    @ExcelProperty({"规则配置", "校验对象描述"})
    @TableField(exist = false)
    @ExcelValid(message = "【规则列表】【规则配置】【校验对象描述】不能为空")
    private String describes;
    /**
     * 业务编码
     */
    @ExcelProperty({"规则配置", "业务编码"})
    @TableField("RULE_CODE")
    @ExcelValid(message = "【规则列表】【规则配置】【业务编码】不能为空")
    private String ruleCode;


    /**
     * 规则名称
     */
    @ExcelProperty({"规则配置", "规则名称"})
    @ColumnWidth(15)
    @TableField("CHECK_NAME")
    @ExcelValid(message = "【规则列表】【规则配置】【规则名称】不能为空")
    private String checkName;

    /**
     * 表达式_过滤
     */
    @ExcelProperty({"规则配置", "校验规则"})
    @TableField("EXPRESS")
    @ExcelValid(message = "【规则列表】【规则配置】【校验规则】不能为空")
    private String express;


    /**
     * 数据集表ID_外键
     */
    @ExcelIgnore
    @TableField("DATASET_ID")
    private String datasetId;

    /**
     * 校验对象ID_外键
     */
    @ExcelIgnore
    @TableField("CHECK_OBJECT_ID")
    private String checkObjectId;

    /**
     * 规则分类ID_外键
     */
    @ExcelIgnore
    @TableField("CHECK_TYPE_ID")
    private String checkTypeId;

    @ExcelProperty({"规则配置", "SQL"})
    @TableField(exist = false)
    private String sql;

    @ExcelProperty(value = {"规则配置", "校验失败编码"}, converter = StringConverter.class)
    @EasyExcel(readConverterExp = "1=错误,2=警告,3=提示,4=通过")
    @TableField("ERROR_CODE")
    @ExcelValid(message = "【规则列表】【规则配置】【校验失败编码】不能为空")
    private String errorCode;
    /**
     * 校验失败提示内容
     */
    @ColumnWidth(20)
    @ExcelProperty({"规则配置", "校验失败提示内容"})
    @TableField("ERROR_MSG")
    @ExcelValid(message = "【规则列表】【规则配置】【校验失败提示内容】不能为空")
    private String errorMsg;

    /**
     * 行级、列级校验状态？
     */
    @ExcelProperty(value = {"规则配置", "行或表校验"}, converter = IntegerConverter.class)
    @EasyExcel(readConverterExp = "1=行级校验,2=表级校验")
    @TableField("ROW_FLAG")
    @ExcelValid(message = "【规则列表】【规则配置】【行或表校验】不能为空")
    private Integer rowFlag;

    @ExcelIgnore
    @TableField(exist = false)
    private String ruleLabelId;

    @ColumnWidth(20)
    @ExcelProperty({"规则配置", "标签"})
    @TableField(exist = false)
    @ExcelValid(message = "【规则列表】【规则配置】【标签】不能为空")
    private String ruleLabels;
    /**
     * 规则详情
     */
    @ColumnWidth(20)
    @ExcelProperty({"规则配置", "详细描述"})
    @TableField("CHECK_DETAILS")
    @ExcelValid(message = "【规则列表】【规则配置】【详细描述】不能为空")
    private String checkDetails;
    /**
     * 校验对象字段
     */
    @ExcelProperty({"规则配置", "校验字段"})
    @TableField("OBJECT_FILED")
    private String objectFiled;

    @ExcelProperty(value = {"规则配置", "尾差状态"}, converter = IntegerConverter.class)
    @EasyExcel(readConverterExp = "0=正常,1=汇总_明细,2=明细_明细")
    @TableField("DIFF_FLAG")
    private Integer diffFlag;


    /**
     * 表达式_执行
     */
    @ExcelIgnore
    @TableField("EXPRESS_RUN")
    private String expressRun;

    /**
     * 渲染页面内容(如果)
     */
    @ExcelIgnore
    @TableField("VIEW_JSON")
    private String viewJson;

    /**
     * 函数名称集合
     */
    @ExcelIgnore
    @TableField("FUNC_NAMES")
    private String funcNames;


    /**
     * 启停状态
     */
    @ExcelIgnore
    @TableField("RUN_FLAG")
    private Long runFlag;

    /**
     * 校验通过状态
     */
    @ExcelIgnore
    @TableField("CHECK_FLAG")
    private Long checkFlag;

    /**
     * 排序
     */
    @ExcelIgnore
    @TableField("SORTS")
    private Integer sorts;

    /**
     * 创建人
     */
    @ExcelIgnore
    @TableField("CREATE_USER")
    private String createUser;

    /**
     * 修改人
     */
    @ExcelIgnore
    @TableField("UPDATE_USER")
    private String updateUser;

    /**
     * 状态(逻辑删除状态)
     */
    @ExcelIgnore
    @TableField("STATUS")
    private Integer status;

    /**
     * 渲染页面内容(则)
     */
    @ExcelIgnore
    @TableField("VIEW_JSON2")
    private String viewJson2;


    /**
     * 数据编码
     */
    @ExcelIgnore
    @TableField("RW_DB_ID")
    private String rwDbId;

    @ExcelIgnore
    @TableField(exist = false)
    private List<String> labelList = null;
    public static final String LABEL_SEPERATOR = "，";

    // 追加标签
    public void addLabel(String label) {
        if (StringUtils.isBlank(label)) {
            return;
        }
        if (null == labelList) {
            labelList = new ArrayList<String>();
        } else if (labelList.contains(label)) {
            return;
        }
        labelList.add(label);
        this.ruleLabels = StringUtils.join(labelList, LABEL_SEPERATOR);
    }

}
